<template>
  <div>
    <van-button
            class="follow-btn"
            type="info"
            color="#3296fa"
            round
            size="small"
            icon="plus"
            v-if="!isFollowed"
            @click="onFollowed"
            :loading ="isFollowLoading"
          >关注</van-button>
          <van-button
          v-else
            class="follow-btn"
            round
            size="small"
            @click="onFollowed"
            :loading ="isFollowLoading"
          >已关注</van-button>
  </div>
</template>

<script>
import { followUser, deleteUser } from '@/api/user.js'

export default {
  props: {
    isFollowed: {
      type: Boolean,
      required: true
    },
    autId: {
      type: [Number, String, Object],
      required: true
    }
  },
  model: {
    prop: 'isFollowed',
    event: 'updateFollow'
  },
  data () {
    return {
      isFollowLoading: false
    }
  },

  created () {

  },

  methods: {
    async onFollowed () {
      this.isFollowLoading = true
      try {
        if (this.isFollowed) {
        // console.log('点击取消关注')
          await deleteUser(this.autId)
        } else {
        // console.log('点击关注')
          await followUser(this.autId)
        }
        // this.isFollowed = !this.isFollowed
        this.$emit('updateFollow', !this.isFollowed)
        this.isFollowLoading = false
      } catch (err) {
        this.isFollowLoading = false
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
